package com.anolesoft.twelvePlan.controller;

import cn.hutool.core.bean.BeanUtil;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.twelvePlan.dto.PlanFenXiDTO;
import com.anolesoft.twelvePlan.dto.PlanFenXiJxDTO;
import com.anolesoft.twelvePlan.service.PlanFenXiService;
import com.anolesoft.twelvePlan.vo.PlanFenXiVO;
import com.anolesoft.yr.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zyq
 * @date 2022/7/18
 * 厂级月份计划预分析
 */
@RestController
@RequestMapping(value="/planFenXi")
@Slf4j
public class PlanFenXiController {
    @Autowired
    private PlanFenXiService planFenXiService;

    /**
     * 条件查询
     * @param planFenXiVO
     * @return
     */
    @PostMapping(value = "/query")
    public AjaxResult queryFenXi(@RequestBody PlanFenXiVO planFenXiVO){
        log.debug("PlanFenXiVO: {}",planFenXiVO);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        planFenXiVO.setFactoryId(loginUser.getFacCode());
        //机型数组处理
        planFenXiVO.setJx(arrayToString(planFenXiVO.getJxArray()));
        //计划等级数组处理
        planFenXiVO.setPlangrade(arrayToString(planFenXiVO.getPlangradeArray()));

        PlanFenXiDTO planFenXiDTO = BeanUtil.copyProperties(planFenXiVO,PlanFenXiDTO.class);
        log.debug("PlanFenXiDTO: {}",planFenXiDTO);
        List<PlanFenXiDTO> planFenXiDTOS = this.planFenXiService.queryFenXi(planFenXiDTO);
        List<PlanFenXiVO> planFenXiVOS = planFenXiDTOS.stream()
                .map(t -> BeanUtil.copyProperties(t,PlanFenXiVO.class))
                .collect(Collectors.toList());
        return AjaxResult.success("查询成功", planFenXiVOS);
    }

    /**
     * 查询页面机型下拉选
     * 数据源：mes_plan_xiada_jixing每月已下达机型表
     * @param planFenXiJxDTO
     * @return
     */
    @PostMapping("/selectJx")
    public AjaxResult selectJx(@RequestBody PlanFenXiJxDTO planFenXiJxDTO) {
        log.debug("planFenXiJxDTO: {}", planFenXiJxDTO);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        planFenXiJxDTO.setFactoryId(loginUser.getFacCode());
        List<PlanFenXiJxDTO> planFenXiJxDTOS = this.planFenXiService.selectJx(planFenXiJxDTO);
        return AjaxResult.success(planFenXiJxDTOS);
    }

    /**
     * 下拉选多选数组转模糊查询字符串
     * @param array
     * @return
     */
    private String arrayToString(String[] array) {
        StringBuffer str = new StringBuffer();
        if (array != null && array.length > 0) {
            int length = array.length;
            for (int i = 0; i < length; i++) {
                String s = array[i];
                if (i < length - 1) {
                    str.append("'" + s + "',");
                } else {
                    str.append("'" + s + "'");
                }
            }
            log.debug("字符串数组转字符串结果：" + str.toString());
            return str.toString();
        } else {
            return "";
        }
    }
}
